import variable from '@/data/variable.json'
import columns from '@/data/columns.json'
import formEditBox from '@/data/formEditBox.json'
import formPostUrl from '@/data/formPostUrl.json'
export default {
	name: 'menuCate',
	components: {},
	// watch: {
	// 	'$route'(to, from) {
	// 		console.log(123);
	// 	}
	// },
	data() {
		return {
			columns: columns[this.$route.name] ? columns[this.$route.name] : null,
			dictionary: variable.dictionary,
			deleteItem: null,
			data: null,
			resType: 0,
			pageNum: 1,
			limit: 10,
			total: 0,
			totalPage: 0,
			list: [],
			listView: [],
			showInquire:false,
			showbackHistory:false,
			downloadBtn:false,
			//详情数据
			Detailform:[{
				input:''
			}],
		
			// 搜索栏数据
			productModelform: {
				effective: '',
				productCode:'',
				state:'',
				tag:''
			},
			effectives:[{
				index:1,
				type:'是'
			},{
				index:2,
				type:'否'
			}],
			states:[{
				index:1,
				type:'上架'
			},{
				index:2,
				type:'下架'
			}],
			tags:[{
				index:1,
				type:'男装'
			},{
				index:2,
				type:'女装'
			},{
				index:3,
				type:'童装'
			}],
			// 编辑框是否打开
			editBox: false,
			// 编辑框数据
			formEditBox: formEditBox[this.$route.name].value ? formEditBox[this.$route.name].value : null,
			formEditBoxInfo: formEditBox[this.$route.name].info ? formEditBox[this.$route.name].info : null,
			tableAction: {
				title: '操作',
				key: 'action',
				fixed: 'right',
				minWidth: 230,
				render: (h, params) => {
					return h('div', {
						'class': 'm-my-1 m-ta-c'
					}, [
						h('Button', {
							props: {
								type: 'info',
								size: 'small',
								ghost: true,
							},
							'class': 'm-mr-2',
							'style':this.$route.params.id ? 'display:none':'display:inline-block',
							on: {
								click: () => {
									this.levelChild(params.row.id)
								}
							}
						}, '搭配详情'),
						h('Button', {
							props: {
								type: 'info',
								size: 'small',
								ghost: true,
							},
							'class': 'm-mr-2',
							on: {
								click: () => {
									this.editBoxOpen(params.index)
								}
							}
						}, '修改'),
						h('Button', {
							props: {
								type: 'error',
								size: 'small',
								ghost: this.deleteItem === params.index ? false : true,
							},
							on: {
								click: () => {
									console.log(params.index);
									if (this.deleteItem !== params.index) {
										console.log("111");
										this.deleteItem = params.index
									} else {
										console.log('222')
										this.delItem(this.list[this.deleteItem].id)
									}
								}
							}
						}, this.deleteItem === params.index ? '确定' : '删除'),
						// h('Button', {
						// 	props: {
						// 		type: 'default',
						// 		size: 'small'
						// 	},
						// 	'class': 'm-mt-1'
						// }, '查看关联商品')
					]);
				}
			},
		}
	},
	
	methods: {
		backHistory(){
			this.$router.go(-1);
		},
		levelChild(id){
			console.log(id)
			this.$router.push({name:'productModelDetailList',params:{id:id}})
		},
		pageLoad(params) {
			console.log(formPostUrl[this.$route.name].loadList);
			this.loading = true;
			params ? params : this.$route.params.id ? params = {id:this.$route.params.id} :params = {
				pageNum: this.pageNum,
				pageSize: this.limit,
			} ;
			this.loading = true;
			let loadList = this.$route.params.id ? formPostUrl[this.$route.name].loadList : formPostUrl[this.$route.name].loadList;
			this.axios.post(loadList,params).then((response) => {
				let res = response.data
				if (res.code === 0 || res.code === '200') {
					if (res.code === 0) {
						this.resType = 1
					} else if (res.code === '200') {
						this.resType = 2
					}

					// 获取并替换显示内容
					let data
					if (res.data) {
						// 数据
						data = res.data.datas
						// 页数
						this.total = res.data.total
						this.totalPage = res.data.totalPage
					} else if (res.result.list) {
						// 数据
						data = res.result.list
						// 页数
						this.total = res.result.total
						this.totalPage = res.result.pages
					}else{
						// 数据
						data = res.result
					}
					// 数据处理与赋值
					this.list = data
					this.listView = data.map(item => _.mapValues(item, (value, key) => _.get(this.dictionary, key + '.' + value, value)))
				} else {
					this.$Notice.error({
						title: '错误',
						desc: res.message
					});
				}
			})
		},
		pageReload(str,pageNum) {			
			console.log('页面重载:',str);
			let params = {};
			if(str == 'productModelList'){
				params = this.productModelform;
			}else if (str == 'talkHistoryList'){
				params = this.talkHistoryform;
			}else if (str == 'goods'){
				params = this.goodsform
			}
			this.pageNum = pageNum ? pageNum : 1;
			params.pageNum = this.pageNum;
			params.pageSize = this.limit;
			this.pageLoad(params);
		},
		pageChange(num) {
			// 改变页码
			this.pageNum = num
			this.pageLoad()
		},
		pageSizeChange(num) {
			// 改变你每页显示条数
			this.limit = num
			this.pageLoad()
		},
		editBoxOpen(num) {
			// 打开编辑框
			console.log('num' + num)
			this.editBox = true
			if (num !== null && num !== undefined) {
				this.formEditBox = this.list[num]
			}
		},
		editBoxClose() {
			// 关闭编辑框
			this.editBox = false
			// 清空editBox
			console.log('this.formEditBox')
			console.log(this.formEditBox)
			for (let o in this.formEditBox) {
				this.formEditBox[o] = null
			}
		},
		editBoxSubmit() {
			console.log('formPostUrl[this.$route.name].editItem'+formPostUrl[this.$route.name])
			if (!formPostUrl[this.$route.name] || !formPostUrl[this.$route.name].editItem) {
				console.log('return false')
				return false
			}
			// 编辑框提交
			let hasid = t.formEditBox.id ? true : false
			this.axios.post(
				hasid ? formPostUrl[this.$route.name].editItem : formPostUrl[this.$route.name].addItem,
				hasid ? this.formEditBox : _.omit(t.formEditBox, ['id'])
			).then((response) => {
				console.log('res')
				let res = response.data
				console.log(res)
				if (res.code === '200') {
					// 刷新列表
					this.pageReload('', this.pageNum)
					// 关闭编辑框
					this.editBoxClose()
				} else {
					this.$Notice.error({
						title: '错误',
						desc: res.message
					});
				}
			})
		},
		delItem(num) {
			if (!formPostUrl[this.$route.name] || !formPostUrl[this.$route.name].delItem) {
				console.log("333")
				return false
			}
			this.axios.post(formPostUrl[this.$route.name].delItem,{id:num}).then((response) => {
				let res = response.data
				if (res.code === 0) {
					// 刷新列表
					this.pageReload('', this.pageNum)
					this.$Notice.success({
						title: '删除成功',
						desc: '您已成功删除: <b>' + this.list[this.deleteItem].name + '</b>'
					});
					this.deleteItem = null
				} else {
					this.$Notice.error({
						title: '错误',
						desc: res.message
					});
				}
			})
		},
		downloadData(){
			console.log(this.formSearchPDRecord);
			let params ={
				pageNum: this.pageNum,
				pageSize: this.limit,
				startDate:this.formSearchPDRecord.startDate,
				endDate:this.formSearchPDRecord.endDate,
				voiceSource:this.formSearchPDRecord.voiceSource,
				isPrecise:this.formSearchPDRecord.isPrecise,
				nickname:this.formSearchPDRecord.nickname,
				type:this.formSearchPDRecord.type,
				productName:this.formSearchPDRecord.productName,
				productId:this.formSearchPDRecord.productId,
			}
			this.axios.post(formPostUrl[this.$route.name].loadList,params).then((response) => {
				let res = response.data
				console.log(res)
				// if (res.code === 0 || res.code === '200') {
				// 	if (res.code === 0) {
				// 		this.resType = 1
				// 	} else if (res.code === '200') {
				// 		this.resType = 2
				// 	}

				// 	// 获取并替换显示内容
				// 	let data
				// 	if (res.data) {
				// 		// 数据
				// 		data = res.data.datas
				// 		// 页数
				// 		this.total = res.data.total
				// 		this.totalPage = res.data.totalPage
				// 	} else if (res.result) {
				// 		// 数据
				// 		data = res.result.list
				// 		// 页数
				// 		this.total = res.result.total
				// 		this.totalPage = res.result.pages
				// 	}
				// 	// 数据处理与赋值
				// 	this.list = data
				// 	this.listView = data.map(item => _.mapValues(item, (value, key) => _.get(this.dictionary, key + '.' + value, value)))
				// } else {
				// 	this.$Notice.error({
				// 		title: '错误',
				// 		desc: res.message
				// 	});
				// }
			})
		}
	},
	mounted() {
		window.t = this
		console.log(this.$route.name);
		// 没找到列表action的话则添加
		if (!_.findKey(this.columns, 'render')) {
			this.columns.push(this.tableAction)
		}
		if(this.$route.name == 'backendProductImage'){
			this.showInquire = true;
			this.pageLoad()
		}else if(this.$route.name == 'talkHistoryList'){
			this.downloadBtn = true;
			this.pageLoad()
		}else{
			this.pageLoad()
		}
	},
	created(){
		console.log(this.$route.name);
		if(this.$route.name == 'productModelDetailList'){
			this.showbackHistory = true;
		}else{
			this.showbackHistory = false;
		}
	}
}
